/**
 * @file ble_key.h
 * @brief
 * @author  xiaowine (xiaowine@sina.cn)
 * @version 01.00
 * @date    2021-01-16
 *
 * @copyright Copyright (c) {2020}  xiaowine
 *
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version <th>Author  <th>Description
 * <tr><td>2021-01-16 <td>1.0     <td>wangh     <td>内容
 * </table>
 * ******************************************************************
 * *                   .::::
 * *                 .::::::::
 * *                ::::::::::
 * *             ..:::::::::::
 * *          '::::::::::::
 * *            .:::::::::
 * *       '::::::::::::::..        女神助攻,流量冲天
 * *            ..::::::::::::.     永不宕机,代码无bug
 * *          ``:::::::::::::::
 * *           ::::``:::::::::'        .:::
 * *          ::::'   ':::::'       .::::::::
 * *        .::::'      ::::     .:::::::'::::
 * *       .:::'       :::::  .:::::::::' ':::::
 * *      .::'        :::::.:::::::::'      ':::::
 * *     .::'         ::::::::::::::'         ``::::
 * * ...:::           ::::::::::::'              ``::
 * *```` ':.          ':::::::::'                  ::::.
 * *                   '.:::::'                    ':'````.
 * ******************************************************************
 */
#ifndef __BLE_KEY_H
#define __BLE_KEY_H

#include <rtthread.h>

typedef struct
{
    unsigned char b0 : 1;
    unsigned char b1 : 1;
    unsigned char b2 : 1;
    unsigned char b3 : 1;
    unsigned char b4 : 1;
    unsigned char b5 : 1;
    unsigned char b6 : 1;
    unsigned char b7 : 1;
} _FLAG_bits;

typedef union
{
    _FLAG_bits bits;
    unsigned char byte;
} _TKS_FLAGA_type;

typedef struct
{
    unsigned char s0 : 2;
    unsigned char s1 : 2;
    unsigned char s2 : 2;
    unsigned char s3 : 2;
} _STATE_2bits;

typedef struct
{
    unsigned char s0 : 4;
    unsigned char s1 : 4;
} _STATE_4bits;

typedef union
{
    _STATE_2bits s2bits;
    _STATE_4bits s4bits;
    unsigned char byte;
} _USR_FLAGA_type;
typedef struct
{
    unsigned char mode : 1;
    unsigned char bits3 : 3;
    unsigned char bits4 : 4;
} _BEEP_bits;
typedef union
{
    _BEEP_bits bits;
    unsigned char byte;
} _BEEP_STATE;
enum
{
    STATE_LED_OFF,
    STATE_LED_ON,
    STATE_LED_FLASH_2HZ,
    STATE_LED_FLASH_1HZ,
    STATE_LED_FLASH_0_5HZ,
    STATE_LED_FLASH_1_T,  //闪烁一下
    STATE_LED_FLASH_2_T,  //闪烁两下
    STATE_LED_FLASH_3_T,  //闪烁三下
};

enum
{
    IDELTEM,
    BOILINGTEM,
    NORMALTEM,
    TEATEM,
    MILKTEM,
    KEY_TEMP = 0x1000,
};

enum
{
    CMD_IDEL     = 0,
    CMD_KEY      = 1,
    CMD_LED      = 2,
    CMD_REG_UP   = 3,
    CMD_REG_DOWN = 4,
    CMD_AUX_DIAI = 5,
    CMD_AUX_DO   = 6,
    CMD_PARA     = 7,
    CMD_SEGMENT  = 8,
    CMD_RGBLEVEL = 9,
};
typedef struct
{
    unsigned char red;
    unsigned char green;
    unsigned char blue;
} Color_st;
typedef struct
{
    unsigned char level;
    Color_st color[3];
} rgbLevel_t;

#define KEYBYTENUM 3
extern _TKS_FLAGA_type keyState[KEYBYTENUM];
extern _TKS_FLAGA_type keyUpState[KEYBYTENUM];
extern volatile _TKS_FLAGA_type keyTrg[KEYBYTENUM];
extern volatile _TKS_FLAGA_type keyUpTrg[KEYBYTENUM];

// #define L28_2_HOT_EDITION
#define L28_2COLD_EDITION
#if defined(L28_2_HOT_EDITION) && defined(L28_2COLD_EDITION)
#error "shoud choose only one EDITION"
#endif
#if (!defined(L28_2_HOT_EDITION)) && (!defined(L28_2COLD_EDITION))
#error "must choose one EDITION"
#endif

#ifdef L28_2_HOT_EDITION
#define HOT_COLD_EDITION 0
#endif
#ifdef L28_2COLD_EDITION
#define HOT_COLD_EDITION 1
#endif

#define BLEON keyState[0].bits.b0
#define BLEONTrg keyTrg[0].bits.b0
#define PARAOK keyState[0].bits.b1
#define PARAOKTrg keyTrg[0].bits.b1
#define BLEINITDONE keyState[0].bits.b2
#define BLEINITDONETrg keyTrg[0].bits.b2
#define BOARDRGB keyState[0].bits.b3
#define BOARDRGBTrg keyTrg[0].bits.b3

#define KEY0 keyState[1].bits.b0
#define KEY1 keyState[1].bits.b2
#define KEY2 keyState[1].bits.b1

#define KEY0Restain keyState[2].bits.b0
#define KEY1Restain keyState[2].bits.b2
#define KEY2Restain keyState[2].bits.b1

#define KEY0Trg keyTrg[1].bits.b0
#define KEY1Trg keyTrg[1].bits.b2
#define KEY2Trg keyTrg[1].bits.b1

#define KEY0RestainTrg keyTrg[2].bits.b0
#define KEY1RestainTrg keyTrg[2].bits.b2
#define KEY2RestainTrg keyTrg[2].bits.b1

#define KEY0UpTrg keyUpTrg[1].bits.b0
#define KEY1UpTrg keyUpTrg[1].bits.b2
#define KEY2UpTrg keyUpTrg[1].bits.b1

#define KEY0UpRestainTrg keyUpTrg[2].bits.b0
#define KEY1UpRestainTrg keyUpTrg[2].bits.b2
#define KEY2UpRestainTrg keyUpTrg[2].bits.b1

#define hotWaterOutKey KEY0
#define hotHeatKey KEY1
#define hotWaterScrKey KEY2

#define hotWaterOutKeyRestain KEY0Restain
#define hotHeatKeyRestain KEY1Restain
#define hotWaterScrKeyRestain KEY2Restain

#define hotWaterOutKeyTrg KEY0Trg
#define hotHeatKeyTrg KEY1Trg
#define hotWaterScrKeyTrg KEY2Trg

#define hotWaterOutKeyRestainTrg KEY0RestainTrg
#define hotHeatKeyRestainTrg KEY1RestainTrg
#define hotWaterScrKeyRestainTrg KEY2RestainTrg

#define hotWaterOutUpKeyTrg KEY0UpTrg
#define hotHeatUpKeyTrg KEY1UpTrg
#define hotWaterScrUpKeyTrg KEY2UpTrg

#define hotWaterOutKeyRestainUpTrg KEY0RestainUpTrg
#define hotHeatKeyRestainUpTrg KEY1RestainUpTrg
#define hotWaterScrKeyRestainUpTrg KEY2RestainUpTrg

#define coldFilterKey KEY0
#define coldElementKey KEY1
#define coldWaterOutKey KEY2

#define coldFilterKeyRestain KEY0Restain
#define coldElementKeyRestain KEY1Restain
#define coldWaterOutKeyRestain KEY2Restain

#define coldFilterKeyTrg KEY0Trg
#define coldElementKeyTrg KEY1Trg
#define coldWaterOutKeyTrg KEY2Trg

#define coldFilterKeyRestainTrg KEY0RestainTrg
#define coldElementKeyRestainTrg KEY1RestainTrg
#define coldWaterOutKeyRestainTrg KEY2RestainTrg

#define coldFilterUpKeyTrg KEY0UpTrg
#define coldElementUpKeyTrg KEY1UpTrg
#define coldWaterOutUpKeyTrg KEY2UpTrg

#define coldFilterKeyRestainUpTrg KEY0RestainUpTrg
#define coldElementKeyRestainUpTrg KEY1RestainUpTrg
#define coldWaterOutKeyRestainUpTrg KEY2RestainUpTrg

extern _USR_FLAGA_type ledState[2];
#define led00State ledState[0].s4bits.s0
#define led01State ledState[0].s4bits.s1
#define led02State ledState[1].s4bits.s0
#define led03State ledState[1].s4bits.s1

#define hotWaterSrcLedState led00State
#define hotWaterOutLedState led01State
#define hotHeaterLedState led02State

#define coldWaterOutLedState led00State
#define coldFilterLedState led01State
#define coldElementLedState led02State

#define waterLevelLedState led03State

extern _BEEP_STATE beepState;
#define BEEPMODE beepState.bits.mode
#define BEEPLONG beepState.bits.bits3
#define BEEPSHORT beepState.bits.bits4
#endif
